package com.example.studykotlin.kotlin_actual_combat.Advanced

import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.studykotlin.*
import com.example.studykotlin.databinding.ActivityAdvancedBinding
import com.example.studykotlin.kotlin_actual_combat.BaseActivity.BaseActivity
import com.example.studykotlin.kotlin_actual_combat.Bean.ItemSingleClickMessage
import com.example.studykotlin.kotlin_actual_combat.annotation.EventMode
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Route(path = Path.advanced)
class AdvancedActivity : BaseActivity() {
    private lateinit var binding: ActivityAdvancedBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityAdvancedBinding.inflate(layoutInflater)
        EventBus.getDefault().register(this)
        setContentView(binding.root)
        initView()
        initData()
    }



    /**
     * 初始化应用主界面列表
     */
    private fun initView() {

        binding.recycler.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        binding.recycler.addItemDecoration(
            DividerItemDecoration(
                this,
                DividerItemDecoration.VERTICAL
            )
        )

    }

    private fun initData() {
        val data = mutableListOf(
            "View",
        )
        listAdapter = ListAdapter(data)
        listAdapter.eventMode = EventMode.ADVANCED
        binding.recycler.adapter = listAdapter
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun handleItemSingleClickMessage(message: ItemSingleClickMessage) {
        if (message.eventMode != EventMode.ADVANCED) return
        when (message.position) {
            0 -> {
                Toast.makeText(this, "View", Toast.LENGTH_SHORT).show()
                ARouter.getInstance().build(Path.view).navigation()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}